@echo off
setlocal

:: bootstrap
pushd %~dp0
call _configure.bat
popd


:: start
set branch=%1
set dir=%BRANCHES_DIR%
set docroot=%BRANCH_HOSTING_PATH%

if not defined SVN_BRANCH_DIR (
	set SVN_BRANCH_DIR=%SVN_USER%
)



if [%branch%] equ [trunk] (

	set branch=trunk-Stable

)

:: declare branch
:redefine_branch
if not defined branch (
	
	set /P branch=Enter branch name: 
	goto :redefine_branch
)

:: validate branch
set branch_path=%dir%\%branch%

:: check out branch in case!
set SVN_FULL_BRANCH_URL=%SVN_SCHEME%://%SVN_HOST%/%SVN_BRANCH_URL%/%SVN_BRANCH_DIR%/%BRANCH%
set SVN_CHECKOUT_DIR=%branch_path%

:: try checkout if branch dir do not exist
if not exist %branch_path%\nul (
	echo checking out %branch_path%
	call svn checkout -r HEAD "%SVN_FULL_BRANCH_URL%" "%SVN_CHECKOUT_DIR%" --username "%SVN_USER%" --password "%SVN_PASS%"
)



if not exist %branch_path%\nul (
	echo Branch %branch_path% do not exist
	goto :redefine_branch
)

:: remove current link
if exist %docroot% (

	if exist %docroot%\nul (
	
		rmdir /S /Q %docroot%
		
	) else (
	
		del /F /Q %docroot%
	)
)

:: create link!
call mklink /D %docroot% %branch_path%




goto :end

:show_usage
echo usage: sb [BRANCH_NAME]
goto :eof

:end
endlocal